package com.xxx.car.web.admin;

import com.github.pagehelper.Page;
import com.xxx.car.entity.TbTravelOrder;
import com.xxx.car.entity.vo.request.ReqCarStatistics;
import com.xxx.car.entity.vo.request.ReqConsumeQuery;
import com.xxx.car.entity.vo.request.ReqUserCarQuery;
import com.xxx.car.entity.vo.response.RespCarStatisticsTotal;
import com.xxx.car.entity.vo.response.RespTotalConsume;
import com.xxx.car.entity.vo.response.RespUserConsume;
import com.xxx.car.service.TravelAppealService;
import com.xxx.common.base.enums.AdminErrorEnum;
import com.xxx.common.base.vo.AdminResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by wenjiexu on 2017/7/17.
 */
@Controller
@RequestMapping("/admin/travel")
public class AdminTravelController {

    @Autowired
    private TravelAppealService travelAppealService;

    @RequestMapping("/car")
    public String statisticIndex() {
        return "carStatistic";
    }

    @RequestMapping("/consume")
    public String consumeIndex () {
        return "consumeStatistic";
    }

    @RequestMapping("/consumeList")
    @ResponseBody
    public AdminResponse consumeList(@Valid @ModelAttribute ReqConsumeQuery reqConsumeQuery, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        Page<RespUserConsume> page = travelAppealService.queryConsumeList(reqConsumeQuery);
        return AdminResponse.returnSuccess(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    @RequestMapping("/consumeTotal")
    @ResponseBody
    public AdminResponse consumeTotal(@Valid @ModelAttribute ReqConsumeQuery reqConsumeQuery, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        RespTotalConsume respTotalConsume = travelAppealService.queryConsumerTotal(reqConsumeQuery);
        return AdminResponse.returnSuccess(respTotalConsume);
    }

    @RequestMapping("/carList")
    @ResponseBody
    public AdminResponse carList(@Valid @ModelAttribute ReqCarStatistics reqCarStatistics, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        Page<TbTravelOrder> page = travelAppealService.queryTravel(reqCarStatistics);
        return AdminResponse.returnSuccess(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(), page);
    }

    @RequestMapping("/carTotal")
    @ResponseBody
    public AdminResponse carTotal(@Valid @ModelAttribute ReqCarStatistics reqCarStatistics, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        RespCarStatisticsTotal total = travelAppealService.queryCarTotal(reqCarStatistics);
        return AdminResponse.returnSuccess(total);
    }

}
